package main

import (
	"fmt"
)

// 自己实现的 双端队列
/****************************************/

type Deque struct {
	q []int
}

func deque() *Deque {
	return &Deque{q: make([]int, 0)}
}

func (this *Deque) push_back(key int) {
	this.q = append(this.q, key)
}

func (this *Deque) pop_back() {
	this.q = this.q[:len(this.q)-1]
}

func (this *Deque) push_front(key int) {
	this.q = append(this.q[:0], append([]int{key}, this.q[:]...)...)
}

func (this *Deque) pop_front() {
	this.q = this.q[1:len(this.q)]
}

func (this *Deque) back() int {
	return this.q[len(this.q) - 1]
}

func (this *Deque) front() int {
	return this.q[0]
}

func (this *Deque) size() int {
	return len(this.q)
}

func (this *Deque) empty() bool {
	return len(this.q) == 0
}

func (this *Deque) clear() {
	this.q = this.q[:0]
}
/****************************************/

func main() {

	// 定义 deque
	d := deque()
	// 双端队列中插入、删除元素
	d.push_back(20)
	d.push_back(10)
	d.pop_back()
	d.push_front(40)
	d.push_front(30)
	d.pop_front() // d : 40 20
	// 遍历 deque
	for i := 0; i < d.size(); i++ { fmt.Print(d.q[i], " ") }
	fmt.Println()
	// 返回头部和尾部元素
	fmt.Println(d.front())  // 40
	fmt.Println(d.back())  // 20
	// 清空 deque
	d.clear()
	fmt.Println(d.size())  // 0
	fmt.Println(d.empty())  // true
}
